using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeSortingOrder : MonoBehaviour {

	// Use this for initialization
	Canvas canvas;
	Renderer[] renders;
	int[] defaultOrder;
	int order = 0;
	void Start () {
		canvas = GetComponent<Canvas>();
		renders = GetComponentsInChildren<Renderer>();
		defaultOrder = new int[renders.Length];
		for (int i = 0; i < renders.Length; i++) {
			defaultOrder[i] = renders[i].sortingOrder;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (canvas == null || renders == null) return;
		if (order != canvas.sortingOrder)
		{
			order = canvas.sortingOrder;
			for(int i = 0; i < renders.Length; i++)
			{
				renders[i].sortingOrder = order+defaultOrder[i];
			}
		}
	}
}
